<?php
date_default_timezone_set('Asia/Shanghai');

//function recordInfo($data,$x_coord = 60)
//{
//    if ($data['msg'] != 'ok') return false;
//    $msg = [];
//    $lists = $data['data']['item_list'];
//    $number = count($lists);
//    for ($i = 0; $i<$number; $i++) {
//        if ($lists[$i]['itemcoord'][0]['x'] > $x_coord) {
//            //如果消息行的下一行坐标比较小，说明该用户所发消息结束。
//            if (($i < $number -1) && ($lists[$i+1]['itemcoord'][0]['x'] < $x_coord)) {
//                $flag = 1;
//            }
//
//            if ($i = $number - 1) {
//                $flag = 1;
//            }
//            //拼接用户发送的消息
//            $info .= $lists[$i]['itemstring'].'  ';
//
//            //如果$flag = 1，说明用户发送消息结束，需要请消息放入数组
//            if ($flag == 1) {
//                //如果用户发送的消息与关键词匹配，存放消息内容，否则消息内容为空
//                if (filterInfo($info)) {
//                    $msg['info'][] = $info;
//                } else {
//                    $msg['info'][] = '';
//                }
//            }
//
//        } else {
//            $msg['user'][] = $lists[$i]['itemstring'];
//            $flag = 0;
//            $info = '';
//        }
//    }
//    return $msg;
//}



function read_file($dir)
{
    // Open a known directory, and proceed to read its contents
    $fileArr = [];
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            $i = -1;
            while (($file = readdir($dh)) !== false) {
                if ($file == '.' || $file == '..') {
                    continue;
                }
                //判断文件后缀，防止按键精灵有时出错
                if(substr(strrchr($file, '.'), 1) == 'bak') {
                    unlink($dir.$file);
                }

                $i++;
                $fileArr[$i]['filename'] = iconv("gb2312","utf-8",$file);
                $fileArr[$i]['filesize'] = strlen(file_get_contents($dir.$file));
            } 
            closedir($dh);
        }
    }
    return $fileArr;
}

function record_file($filename,$filesize,$db)
{
    $time = date('Y-m-d H:i:s');
    $sql = "update text set size = '$filesize',update_at = '$time' where name = '$filename'";
    $res = $db->query($sql);
    if (!$res) {
        exit('更新失败');
    }
}

function record_business($key,$filename,$text,$db2)
{
    $time = date('Y-m-d H:i:s');
    //sales_customer表中记录的客户名称
    $customer_id = get_customer($filename,$db2);
    //business 表
    $title = $key;
    $last_trace_id = 0;
    $status = 1;
    $insert_id = $db2->insertOne('business','customer_id,title,last_trace_id,status,create_at',"'$customer_id','$title','$last_trace_id','$status','$time'");

    //business_trace表
    if ($insert_id) {
        $user_id = 1;
        $last_trace_id = $db2->insertOne('business_trace', 'business_id,summary,user_id,create_at', "'$insert_id','$text','$user_id','$time'");
    }

    $sql = "update business set last_trace_id = '$last_trace_id' where id = '$insert_id'";
    $db2->query($sql);
}

function get_customer($filename,$db2)
{
    $customer_name = substr($filename,0,-strlen(strrchr($filename,'.')));
    $customer_arr = $db2->getOne('sales_customer',"name='$customer_name'",'id');
    return $customer_arr['id'];
}


function select_file($db)
{
    return $db->getMany('text','1=1','*');
}


function new_text($file,$length)
{
    if (is_file(iconv("utf-8","gb2312",$file))) {
        $text = file_get_contents(iconv("utf-8","gb2312",$file));
        return  substr($text,$length);
    } else {
        exit('文件不存在！');
    }
}

function filter_info($item)
{
    $key = '';
    //匹配品牌
    $brands = require('./match_brand.php');
    foreach($brands as $k=>$b) {
        if (strpos($item,$b) !== false) {
            $key .= $b.' ';
            break;
        }
    }

    //匹配型号
    $brands = require('./match_model.php');
    foreach($brands as $k=>$m) {
        if (strpos($item,$m) !== false) {
            $key .= $m.' ';
            break;
        }
    }
    //匹配配件名称
    $parts = require('./match_part.php');
    foreach ($parts as $k=> $p) {
        if (strpos($item,$p) !== false) {
            $key .= $p.' ';
            break;
        }
    }

    //匹配配件编号
    $preg = explode("\r\n",trim($item));
    array_splice($preg,0,1);
    if (preg_match('/[0-9a-zA-Z]{7,11}/',implode('',$preg),$match)) {
        $key .= $match[0].' ';
    }
    //将文本分词，匹配品牌、型号、配件名称
//    $contrast = array_merge(require('./match_brand.php'),require('./match_model.php'),require('./match_part.php'));
//    $info_arr = psc($text);
//    $diff = array_intersect($contrast,$info_arr);
//    if (!empty($diff)) {
//        return true;
//    }
    return $key;
}